

import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;

public class TextTool extends Tool{

	  protected Point startingPosition;
	  protected StringBuffer text;
	  protected Font font = new Font("Serif", Font.BOLD, 24);
	  protected TextObject tobj;

	  public TextTool(DrawingCanvas canvas, LinkedList<ShapeObject> ShapeObjects, MDUser mdUser) {
		  super(canvas, ShapeObjects, mdUser);
	  }
	  
	 public void mousePressed(MouseEvent e)  {
		 canvas.requestFocus();
		 startingPosition = e.getPoint();
		 int yoffset = canvas.getGraphics().getFontMetrics(font).getHeight();
		 int ey = startingPosition.y - yoffset; 
		 Graphics iBGraphics = canvas.getImageBufferGraphics();
		 iBGraphics.setFont(font);
		 text = new StringBuffer();
		 tobj = new TextObject(new Point(startingPosition.x,ey), startingPosition, canvas.penColor,text);
		 ShapeObjects.add((ShapeObject)tobj);
	 }

	  public void keyPressed(KeyEvent e)  {
//		  if enter is pressed then create object on server
		  if(e.getKeyCode()==10){
			  paintObjects(0);
		  }else{
			  char nextChar = e.getKeyChar(); 
			  tobj.text.append(nextChar);
			  int ex = canvas.getGraphics().getFontMetrics(font).charWidth(nextChar);
			  tobj.endingMousePosition.x += ex;
			  Graphics iBGraphics = canvas.getImageBufferGraphics();
			  iBGraphics.drawString(text.toString(), startingPosition.x,
					  startingPosition.y); 
			  paintObjects(1);
		  }
	  }

	   public void mouseReleased(MouseEvent e) { } 
	   public void mouseDragged(MouseEvent e) { }
}
